<extend name="Base/common"/>
<block name="head_css">
<link href="__PUBLIC__/Green/css/usercenter.css" rel='stylesheet' type='text/css' />
</block>
<block name="head_script">
    <script type="text/javascript" src="__PUBLIC__/Green/js/usercenter.js"></script>
</block>
<block name="body">
    <div class="width1000">
         <div class="top_tit">
            <a href="{:U('User/index')}">用户中心</a> &gt;&gt; 我的账号 &gt;&gt; 个人资料
        </div>
        <div class="clearfix relative">
            <include file="User/nav" />
            <div class="user_center">
                <div class="user_profit">
                    <div class="profile_tit">
                        个人资料
                    </div>
                     <div class="user_add">
                    	<a class="user_add_btn">修改</a>
                    </div>
                    <div id="info_person">
                    <div class="profile_person">
                            <div class="profile_list clearfix" style="height: 35px">
                                <div class="span3 f_l t_r">
                                    <span class="profile_list_pad">
                                        姓名：
                                    </span>
                                </div>
                                <div class="span7 f_l" id="realname"><span id="user_realname">{$user.realname}</span></div>
                            </div>
                            <div class="profile_list clearfix" style="height: 35px">
                                <div class="span3 f_l t_r">
                                    <span class="profile_list_pad">
                                        性别：
                                    </span>
                                </div>
                                <div class="span7 f_l" id="sex"><span id="user_sex"><eq name="user['sex']" value="0">女<else />男</eq></span></div>
                                
                            </div>
                            <div class="profile_list clearfix">
                                <div class="span3 f_l t_r">
                                    <span class="profile_list_pad">
                                        手机号：
                                    </span>
                                </div>
                                <div class="span7 f_l" id="mobile">{$user.mobile}</div>
                            </div>
                            <div class="profile_list clearfix">
                                <div class="span3 f_l t_r">
                                    <span class="profile_list_pad">
                                        身份证：
                                    </span>
                                </div>
                                <div class="span7 f_l">
                                    <span id="user_ident_no">{$user.ident_no|hidebypass=###,0,14}</span>
                                </div>
                            </div>
                    </div>
                    </div>
                    <div class="address_detail" id="addr_list_div">
                        <table class="table table-hover">
                            <thead>
                                <th>收件地址</th>
                                <th>收件人</th>
                                <th>联系电话</th>
                                <th>操作</th>
                            </thead>
                            <tbody id="info_addr_list">
                            <volist name="address" id="vo">
                                <tr>
                                <td><span class="i_province">{$vo.province}</span><span class="i_city">{$vo.city}</span><span class="i_area">{$vo.area}</span><span class="i_address">{$vo.address}</span></td>
                                <td><span class="i_recvname">{$vo.recvname}</span></td>
                                <td><span class="i_mobile">{$vo.mobile}</span></td>
                                <td><a href="javascript:void(0);" class="i_edit" data="{$vo.aid}">修改</a> <a href="javascript:void(0);" class="i_del" data="{$vo.aid}">删除</a></td>
                                </tr>
                            </volist>
                            </tbody>
                        </table>
                        <a href="javascript:void(0)" class="i_add btn_add_address">添加新地址</a>
                    </div>
                    <div class="empty_div">&nbsp;</div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="pop_content">
        <div class="new_address">
            <form action="#" method="post" id="formAddr">
                <span class="pop_close"></span>
                <table class="pop_addr_table">
                    <tr>
                        <td class="t_r">省市：</td>
                        <td>
                            <select name="province" id="province" class="required"></select>
                            <select name="city" id="city" class="required"></select>
                            <select name="area" id="area" class="required"></select>
                        </td>
                    </tr>
                    <tr>
                        <td class="t_r">具体地址：</td>
                        <td>
                            <input type="text" name="address" id="address" class="required" size="40" />
                        </td>
                    </tr>
                    <tr>
                        <td class="t_r">联系人：</td>
                        <td>
                            <input type="text" name="recvname" id="recvname" class="required" value="{$user.realname}" />
                        </td>
                    </tr>
                    <tr>
                        <td class="t_r">联系电话：</td>
                        <td>
                            <input type="text" name="mobile" id="mobile" class="required" value="{$user.mobile}" />
                        </td>
                    </tr>
                </table>
                <input type="hidden" name="aid" id="aid" value="" />
                <input type="submit" value="提交" class="add_submit"/>
            </form>
        </div>
    </div>    


    <div class="pop_content_info">
        <div class="new_address">
            <form action="#" id="userInfo"  method="post">
            <span class="pop_close"></span>
                    <div class="profile_person">
                            <div class="profile_list clearfix" style="height: 35px">
                                <div class="span3 f_l t_r">
                                    <span class="profile_list_pad">
                                        姓名：
                                    </span>
                                </div>
                                <div>
                                <input type="text" name="realname" value="{$user.realname}" class="form-control" id="realname" placeholder="输入姓名" style="width: auto; height: auto;" />
                                <label class="inptu_err realname_err" style="float: right; margin-top: -32px; margin-right: 70px;"></label></div>
                            </div>
                            <div class="profile_list clearfix" style="height: 35px">
                                <div class="span3 f_l t_r">
                                    <span class="profile_list_pad">
                                        性别：
                                    </span>
                                </div>
                                <select name="sex" id="sex" class="modify_sex"> 
                                	<option value ="0">女</option>
                                	 <option value ="1">男</option>
                                </select>
                            </div>
                            <div class="profile_list clearfix">
                                <div class="span3 f_l t_r">
                                    <span class="profile_list_pad">
                                        手机号：
                                    </span>
                                </div>
                                <div class="span7 f_l" id="mobile">{$user.mobile}</div>
                            </div>
                            <div class="profile_list clearfix">
                                <div class="span3 f_l t_r">
                                    <span class="profile_list_pad">
                                        身份证：
                                    </span>
                                </div>
                                <div class="span7 f_l">
                              	<eq name="user['ident_no']" value="">
                              		<input type="text" name="ident_no" value="" class="form-control" id="ident_no" placeholder="输入身份证号" style="width: auto; height: auto;" />
                              		<label class="inptu_err"></label>
                              		<else />
                              		
                              		  {$user.ident_no|hidebypass=###,0,14}</eq>
                                    
                                </div>
                            </div>
                    	</div>
                <input type="button" value="保存" onclick="saveinfo()" class="add_submit"/>
            </form>
        </div>
    </div>
</block>    
<block name="script">
<style type='text/css'>
button{
background: #ffbb42 none repeat scroll 0 0;
    border-radius: 2px;
    color: #fff;
    cursor: pointer;
    height: 32px;
    margin-left: 10px;
    padding: 0 15px;
    text-align: center
	
}

</style>
<script type="text/javascript" src="__PUBLIC__/Green/js/insurance.js"></script>
<script type="text/javascript" src="__PUBLIC__/Green/js/jquery.jeditable.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/Green/js/PCASClass.js"></script>
<script type="text/javascript">
    var pcas = new PCAS("province","city","area");
    $(".pop_close").on("click",function(){
            $(".pop_content").hide();
       		$(".pop_content_info").hide();     
    });
    highlight_nav("{:U('User/info')}");
    highlight_nav("javascript:void(0);");
    var saveUrl = "{:U('User/saveinfo')}";
    $("#realname").on("click",function(){
    	 $('#realname').editable(saveUrl,{
    	    	css:"height:0",
    	        submit:"保存"
    	    });
    });
    $('#sex').editable(saveUrl,{
        data:"{'1':'男','0':'女'}",
        type:"select",
        style:"height:0",
        submit:"保存"
    });
    $(".user_add_btn").on("click",function(){
         $('#aid').val('');
         $(".pop_content_info").fadeIn(400);
    	
         
         
           
    });
    
    $(window).load(function() {
    	if({$user.sex}==0){
    		$(".modify_sex").find("option[value='0']").attr("selected",true);
    	}else{
    		$(".modify_sex").find("option[value='1']").attr("selected",true);
    	}
    });
    $('#formAddr').validate({
        submitHandler:function(form){
            $.post("{:U('User/saveAddress')}",$(form).serialize(),function(data){
                if(data.status){
                    loadAddr(data.info);
                    $("#formAddr")[0].reset();
                    $(".pop_content").hide();
                }else{
                    alert(data.info);
                }
            },'json');
        } 
    });
    $(":input").on("focus",function() { 
    	
	    $("."+this.id+'_err').hide();
	});
	$(":input").on("blur",function() {  
		if($(this).val()==''){
	    $("."+this.id+"_err").fadeIn();
		}
	});
    function saveinfo(){
    	<?php if(! $user['ident_no']) {?>
    	if( !IdentityCodeValid($("#ident_no").val())){
    		return;
    	}
    	<?php }?>
    	if($("input[name='realname']").val()==''){
    		$(".realname_err").text("请输入姓名");
    		$(".realname_err").fadeIn();
    		return
    	}
    	$.ajax({
            cache: true,
            type: "POST",
            url:"{:u('User/saveinfo')}",
            data:$('#userInfo').serialize(),
            async: false,
            error: function(XMLHttpRequest, textStatus, errorThrown) {
            	alert(XMLHttpRequest.status);
            },
            success: function(data) {
               if(data.status){
            	   if(data.info.realname){
            		   $("#user_realname").text(data.info.realname);
            		   $("input[name='realname']").val(data.info.realname);
                   }
            	 if(data.info.sex==0){
            		 $("#user_sex").text("女");
            		 $(".modify_sex").find("option[text='女']").attr("selected",true);
            	 }else{
            		 $("#user_sex").text("男");
            		 $(".modify_sex").find("option[text='男']").attr("selected",true);
            	 }
            	 if(data.info.ident_no){
            	 $("#user_ident_no").text(data.info.ident_no);

               }
                 $(".pop_content_info").hide();
            }
            }
        });
    }
    function editAddr(){
        var tr = $(this).closest('tr'),
            province = tr.find('.i_province')[0],
            city = tr.find('.i_city')[0],
            area = tr.find('.i_area')[0],
            address = tr.find('.i_address')[0],
            recvname = tr.find('.i_recvname')[0],
            mobile = tr.find('.i_mobile')[0],
            aid = $(this).attr('data');
        pcas.SetValue($(province).text(),$(city).text(),$(area).text());
        $('#address').val($(address).text());
        $('#recvname').val($(recvname).text());
        $('#mobile').val($(mobile).text());
        $('#aid').val(aid);
        $(".pop_content").fadeIn(300);
    }
    
    function delAddr(){
        var tr = $(this).closest('tr'),
            province = tr.find('.i_province')[0],
            city = tr.find('.i_city')[0],
            area = tr.find('.i_area')[0],
            address = tr.find('.i_address')[0],
            aid = $(this).attr('data'),
            str = $(province).text()+$(city).text()+$(area).text()+$(address).text();
        if(confirm('确认要删除地址\n' + str + '?')){
            $.post("{:U('User/delAddress')}",{aid:aid},function(data){
                if(data.status){
                    loadAddr(data.info);
                }else{
                    alert(data.info);
                }
            },'json');
        }
    }
    
    function loadAddr(info){
        var i=0;
        var html = "";
        for(;i<info.length;i++){
            html += '<tr><td><span class="i_province">'+info[i].province+'</span><span class="i_city">'+info[i].city+'</span><span class="i_area">'+info[i].area+'</span><span class="i_address">'+info[i].address+'</span></td>'+
            '<td><span class="i_recvname">'+info[i].recvname+'</span></td><td><span class="i_mobile">'+info[i].mobile+'</span></td><td><a href="javascript:void(0);" class="i_edit" data="'+info[i].aid+'">修改</a> <a href="javascript:void(0);" class="i_del" data="'+info[i].aid+'">删除</a></td></tr>';
        }
        $('#info_addr_list').html(html);
        $(".i_edit").on("click",editAddr);
        $(".i_del").on("click",delAddr);
    }

    $(".i_edit").on("click",editAddr);
    $(".i_del").on("click",delAddr);
    $(".i_add").on("click",function(){
        $("#formAddr")[0].reset();
        $('#aid').val('');
        $(".pop_content").fadeIn(300);
    });
</script>
</block>
